#!/bin/sh
# script name: 容器日志检测/清理脚本
# author: subushi
# update date: 2023.08.30

echo "select"
echo -e "\033[1;42;37m 1.查看容器日志总和 \033[0m"
echo "--------------------------------------------"
echo -e "\033[1;42;37m 2.清理所有容器日志 \033[0m"

read -p "Input selection 1/2：" choice

case $choice in

  1)
echo "======== start docker containers logs file size ========"
echo "所有容器日志总和为: "
find /var/lib/docker/containers/ -name *-json.log* -exec ls -l {} \; |awk 'BEGIN{sum=0}{sum=sum+$5}END{print sum/1024/1024,"MB"}'
echo "======== end docker containers logs file size ========"
  ;;


  2)
echo "======== start clean docker containers logs ========"
logs=$(find /var/lib/docker/containers/ -name *-json.log)
for log in $logs
do
echo "clean logs : $log"
cat /dev/null > $log
done
echo "======== end clean docker containers logs ========"
  ;;


  *)
  echo "Invalid selection"
  ;;
esac
